package com.PMSys.Service;

import java.util.List;

import com.PMSys.Dao.DepartmentDao;
import com.PMSys.Dao.DepartmentDaoImp;
import com.PMSys.Demain.Department;
import com.PMSys.Demain.Staff;

public class DepartmentService {

	private DepartmentDao departmentDao;

	public DepartmentService() {
		
		departmentDao = new DepartmentDaoImp();
	}

	public boolean add(Department dp) {

		// have superiorId
		if (dp.getD_superiorId() != 0) {
			// superior not exist
			if (departmentDao.selectById(dp.getD_superiorId()) == null) {
				return false;
			}
		}
		// add department successful?
		if (departmentDao.add(dp) != 0) {
			return true;
		}

		return false;

	}

	public boolean update(Department dp) {

		// have superiorId
		if (dp.getD_superiorId() != 0 && dp.getD_id()!=dp.getD_superiorId()) {
			// superior not exist
			if (departmentDao.selectById(dp.getD_superiorId()) == null) {
				return false;
			}
		}
		// add department successful?

		if (departmentDao.update(dp) != 0) {
			return true;
		}

		return false;
	}

	public boolean delete(int id) {
		
//		Staff staff = new Staff();
//		staff.setD_id(""+id);
//		StaffDao staffDao = new StaffDaoImp(DbUtils.getConnection());
//		staffDao.getSelectStaffForDepartmentId(staff);
		
		if (!departmentDao.existSubordinate(id) && !departmentDao.existEmployee(id)) {
			// no Subordinate and Employee
			if (departmentDao.delete(id) != 0) {
				return true;
			}
			
		}
		return false;
	}

	public Department selectById(int id) {
		return departmentDao.selectById(id);
	}

	public Department selectByNumber(String number) {
		return null;
	}

	public List<Department> selectPart(Department dp) {
		return departmentDao.selectPart(dp);
	}

	public List<Department> selectAll() {
		return departmentDao.selectAll();
	}
	
	public List<Staff> selectDepartmentEmployees(int id) {
		return departmentDao.selectDepartmentEmployees(id);
		
	}
}
